package com.wzcl.app.dao.repository;

import com.wzcl.app.dao.model.WorkShow;
import com.wzcl.app.model.dto.WorkShowDto;
import io.swagger.models.auth.In;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public interface WorkShowRepository extends JpaRepository<WorkShow, Integer> , JpaSpecificationExecutor {
    //根据id更新作品秀图片
    @Query(value = "update WorkShow ws set ws.updateTime = ?3, ws.image = ?2 where ws.id = ?1")
    @Modifying
    @Transactional
    void updateImageById(Integer id, String image, Date updateTime);

    //根据年级查找作品秀
    @Query(value = "select ws from WorkShow ws where ws.course.grade.name = ?1")
    ArrayList<WorkShow> findAllByGrandName(String gradeName);

    //查询所有
    @Query(value = "select ws from WorkShow ws")
    ArrayList<WorkShow> findAllWorkShow();

    //根据作品秀id查找点赞人集合
    @Query(value = "select ws from WorkShow ws where ws.id = :id")
    WorkShow findLikeById(@Param("id") Integer id);

    //作品秀审核状态
    @Query(value = "update WorkShow ws set ws.checkStatus = ?2 where ws.id = ?1")
    @Modifying
    @Transactional
    void updateWorkShowCheckStatus(Integer id, Integer checkStatus);

    //根据作品秀id更新点赞数据
    @Modifying
    @Query(value = "update WorkShow ws set ws.virtualLikeNumber = ?1, ws.virtualLikeOfPerson = ?2, ws.updateTime = ?3 where ws.id = ?4")
    @Transactional
    void updateVirtualLikeDataById(Integer virtualLikeNumber, String likeOfPerson, Date updateTime, Integer id);

    @Modifying
    @Query(value = "update WorkShow ws set ws.likeNumber = ?1, ws.likeOfPerson = ?2, ws.likeIconList =?3, ws.updateTime = ?4 where ws.id = ?5")
    @Transactional
    void updateLikeDataById(Integer virtualLikeNumber, String likeOfPerson, String likeIconList, Date updateTime, Integer id);

    // 根据id查找作品秀
    @Query(value = "select ws from WorkShow ws where ws.id = ?1")
    WorkShow findWorkShowById(Integer id);

    //用户id查询作品秀
    @Query(value = "select ws from WorkShow ws where ws.appUser.id = ?1")
    List<WorkShow> findAllByUserId(Integer userId);

    //根据章节id查询作品秀
    @Query(value = "select ws from WorkShow ws where ws.catalog.id = ?1")
    WorkShow findWorkShowByCatalogId(Integer catalogId);

    //根据id更新推送状态
    @Modifying
    @Query(value = "update WorkShow ws set ws.pushStatus = ?1 where ws.id = ?2")
    @Transactional
    void updatePushStatusById(Integer status, Integer id);

    //查询当前课程下已推送的优秀作品
    @Query(value = "select ws from WorkShow ws where ws.pushStatus = 1 and ws.course.id = ?1")
    List<WorkShow> findPushSuccess(Integer courseId);

    //更新点评状态
    @Modifying
    @Query(value = "update WorkShow ws set ws.isComment = ?1 where ws.id = ?2")
    @Transactional
    void updateCommentStatusById(Integer status, Integer id);

    //查询未推送的作品按点赞数排序
    @Query(value = "select ws from WorkShow ws where ws.pushStatus = 0 and ws.course.id = ?1 order by ws.likeNumber desc")
    List<WorkShow> findNotPushByCourseSort(Integer courseId);
}